useFormStatus basics tutorial | Everything you need to know
In this tutorial, we will learn about the basics of useFormStatus. We will cover everything you need to know about useFormStatus
. Let's get started.
Video
Why do we need useFormStatus?
When user submits a form and the form is being processed, it is a good practice to show a loading indicator to the user and disable the submit button. Previously you would create a separate state and manipulate the state inside submit handler. Similar to this:
1const Form = () => {2 const [loading, setLoading] = useState(false)34 const handleSubmit = async e => {5 e.preventDefault()6 setLoading(true)7 // ... your form submission logic8 setLoading(false)9 }1011 return (12 <form onSubmit={handleSubmit}>13 <input type='text' name='name' />14 <button type='submit' disabled={loading}>15 Submit16 </button>17 </form>18 )19}
This is fine but it would have been nice if react could handle this logic for us.
And this is where useFormStatus
comes in.
How to use useFormStatus
?
1// Form.jsx23const Form = () => {4 const handleSubmit = async data => {5 console.log(data.get('name')) // Prints the input value6 // ... your form submission logic7 }89 return (10 <form action={handleSubmit}>11 <input type='text' name='name' />12 <SubmitButton />13 </form>14 )15}
1// SubmitButton.jsx2import { useFormStatus } from 'react-dom'34const SubmitButton = () => {5 const { pending } = useFormStatus()67 return (8 <button type='submit' disabled={pending}>9 Submit10 </button>11 )12}
Explanation:
- Like before we have a
handleSubmit
function but this time we passed inaction
prop. handleSubmit
takes a argument which is theFormData
. You can get the input value using theget
method.- We have a separate submit button. And we use the
useFormStatus
hook. useFormStatus
returns an object with apending
property.pending
istrue
when the form is being submitted andfalse
when the form submission is complete.
useFormStatus
actually returns four things:
pending
:true
when the form is being submitted andfalse
when the form submission is complete.action
: Theaction
function passed to the form.data
: TheFormData
object.method
: The submit method.GET
orPOST
etc.
action
, data
and method
are null when the form is not being submitted.
So, this is how you can use useFormStatus
to handle form submission status.
Things to keep in mind
- Form submission function should be passed to the form using the
action
prop. - The handler function has to be async or return a promise.
useFormStatus
should be used inside a component that is a child of the form.
1// This will not work2const Form = () => {3 const { pending } = useFormStatus()4 return (5 <form>6 <input type='text' name='name' />7 <button type='submit' disabled={pending}>8 Submit9 </button>10 </form>11 )12}1314// This will work15const Form = () => {16 return (17 <form>18 <input type='text' name='name' />19 <SubmitButton />20 </form>21 )22}2324const SubmitButton = () => {25 const { pending } = useFormStatus()26 return (27 <button type='submit' disabled={pending}>28 Submit29 </button>30 )31}
That's it for this tutorial. I hope you find this tutorial helpful. If yes, please subscribe to my channel for more tutorials like this. If you have any questions, feel free to ask in the comments below.
Shameless Plug
I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram : @thatanjan
- Twitter: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: